package Dao_Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import Dao.IStudentDao;
import JavaServlet.TimeServlet.JDBCUtil;
import JavaServlet.TimeServlet.Student;

public class StudentDaoImpl implements IStudentDao{
	
	@Override
	public List<Student> selectAll() {
		ArrayList<Student>list = new ArrayList<Student>();
		ResultSet resultSet = null;
		Connection connection = null;
		Statement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id, name,age,gender from student";
			statement = connection.createStatement();
			resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				//int banjiId = resultSet.getInt("banjiId");
				Student student = new Student(id, name,age,gender);
				list.add(student);
			}
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return list;
	}

	@Override
	public List<Student> selectByName(String searchName) {
		ArrayList<Student>list = new ArrayList<Student>();
		ResultSet resultSet = null;
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id, name,age,gender from student where name = ?";
			statement = connection.prepareStatement(sql);
			statement.setString(1, searchName);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				//int banjiId = resultSet.getInt("banjiId");
			    Student student = new Student(id, name,age,gender);
			    list.add(student);
				
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return list;
	}

	@Override
	public Integer insert(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "insert into student(`name`,age,gender,banji_id)values(?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1,student.getName());
			preparedStatement.setInt(2,student.getAge());
			preparedStatement.setString(3,student.getGender());
			preparedStatement.setInt(4,student.getBanjiId());
			System.out.println(preparedStatement);
			count = preparedStatement.executeUpdate();
			if(count == 1) {
				System.out.println("插入成功");
			}else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;	
	}

	@Override
	public Integer update(Student student) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "update student set name = ?,age = ?,gender = ? where id = ?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1,student.getName());
			preparedStatement.setInt(2,student.getAge());
			preparedStatement.setString(3,student.getGender());
			preparedStatement.setInt(4, student.getId());
			System.out.println(preparedStatement);
			count = preparedStatement.executeUpdate();
			if(count == 1) {
				System.out.println("更新成功");
			}else {
				System.out.println("更新失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}	
		return count;
	}

	@Override
	public Integer deleteById(Integer id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "delete from student where id=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			System.out.println(preparedStatement);
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
	}

	@Override
	public List<Student> searchName(String searchName) {
	ArrayList<Student>list = new ArrayList<Student>();
	ResultSet resultSet = null;
	Connection connection = null;
	PreparedStatement statement = null;
	try {
		connection = JDBCUtil.getConnection();
		String sql = "select id, name, age, gender from student where name like ? ";
		statement = connection.prepareStatement(sql);
		statement.setString(1, "%"+searchName+"%");
		System.out.println(statement);
		resultSet = statement.executeQuery();
		while (resultSet.next()) {
			int id = resultSet.getInt("id");
			String name = resultSet.getString("name");
			int age = resultSet.getInt("age");
			String gender = resultSet.getString("gender");
			//int banjiId = resultSet.getInt("banjiId");
			Student student = new Student(id, name, age, gender);
			list.add(student);
		}
		for (Student student : list) {
			System.out.println(student);
		}
	} catch (SQLException e) {
		e.printStackTrace();
	}finally {
		JDBCUtil.close(connection, statement, resultSet);
	}
	return list;
 }

	@Override
	public Student selectById(int id) {
		ResultSet resultSet = null;
		Connection connection = null;
		PreparedStatement statement = null;
		Student student = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id, name, age, gender from student where id = ?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, id);
			resultSet = statement.executeQuery();
			if (resultSet.next()) {
				int studentId = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				//int banjiId = resultSet.getInt("banjiId");
			    student = new Student(id, name, age, gender);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return student;
	}

//	@Override
//	public Integer getTotalCount() {
//		// TODO Auto-generated method stub
//		return null;
//	}

	
	@Override
	public Integer getTotalCount() {
		//PageBean pageBean = new PageBean();
		//Integer pageSize = pageBean.getPageSize();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select count(*) from student";
			preparedStatement = connection.prepareStatement(sql);
			//System.out.println(preparedStatement);
			resultSet = preparedStatement.executeQuery();
		
			if(resultSet.next()) {
				count = resultSet.getInt(1); // 第一列
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return count;
	}

	@Override
	public List<Student> pageList(int offset, Integer pageSize) {
		ArrayList<Student> list = new ArrayList<Student>();
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id, `name`, age, gender from student limit ?,?";
			statement = connection.prepareStatement(sql);
			statement.setInt(1, offset);
			statement.setInt(2, pageSize);
			System.out.println(statement);
			resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");
				//int banjiId = resultSet.getInt("banjiId");
			    Student student = new Student(id, name,age,gender);
			    list.add(student);	
			}
			
			for (Student student : list) {
				System.out.println(student);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			JDBCUtil.close(connection, statement, resultSet);
		}
		return list;
	}
}
